﻿Imports System.IO

Public Class frmClientDocuments

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        Try
            Me.OpenFileDialog1.Filter = "All files (*.*)|*.*"
            If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

                'Set files
                Dim SelFile As FileInfo
                Dim SelFilePath, DestFilePath As String

                SelFilePath = Me.OpenFileDialog1.FileName
                SelFile = New FileInfo(SelFilePath)
                DestFilePath = FTPAddress + "Clients\" + "C-" & Me.txtClientID.Text.Trim & "\" + SelFile.Name

                My.Computer.Network.UploadFile(SelFilePath, DestFilePath, FTPUserName, FTPPassword, True, 1000)

                FillDocuments()
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub FillDocuments()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim Dir As New IO.DirectoryInfo(FTPAddress + "Clients\" & "C-" & Me.txtClientID.Text.Trim)
            Dim DirFiles As IO.FileInfo() = Dir.GetFiles()

            Me.GridFiles.Rows.Clear()

            'list the names of all files in the specified directory
            For Each Fi As FileInfo In DirFiles
                Me.GridFiles.Rows.Add(New String() {Fi.Name, Fi.CreationTime.ToString("yyyy/MM/dd hh:mm tt"), "Open", "Delete"})
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub GridFiles_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridFiles.CellClick
        If e.ColumnIndex = 2 Then
            'Dim Fi As New FileInfo(FTPAddress + "Claims\" + ClaimNo + "\" + Me.GridFiles.Rows(e.RowIndex).Cells(0).Value)
            'Fi.Open(FileMode.Open, FileAccess.ReadWrite)
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim FilePath As String = FTPAddress + "Clients\" + "C-" & Me.txtClientID.Text.Trim & "\" & Me.GridFiles.Rows(e.RowIndex).Cells(0).Value
                System.Diagnostics.Process.Start(FilePath)

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                MsgBox(ex.ToString)
            End Try

        ElseIf e.ColumnIndex = 3 Then
            If MsgBox("Confirm delete?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                Try
                    Me.Cursor = Cursors.Default

                    Dim Fi As New FileInfo(FTPAddress + "Clients\" & "C-" & Me.txtClientID.Text.Trim & "\" & Me.GridFiles.Rows(e.RowIndex).Cells(0).Value)

                    Fi.Delete()

                    FillDocuments()

                    Me.Cursor = Cursors.Default
                Catch ex As Exception
                    Me.Cursor = Cursors.Default
                    If cnn.State = ConnectionState.Open Then
                        cnn.Close()
                    End If
                    MsgBox(ex.ToString)
                End Try
            End If
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmClientDocuments_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        FillDocuments()
    End Sub
End Class